package com.huike.web.controller.business;

import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.service.ITbBusinessRecordService;

import com.huike.clues.service.ISysDictDataService;
import com.huike.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVO;
import com.huike.business.service.ITbBusinessRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: dw
 * @Date 2022/3/31 22:31
 */
@RestController
@RequestMapping("/business/record")
public class TbBusinessRecordController {

    @Autowired(required = false)
    private ITbBusinessRecordService iTbBusinessTrackRecordService;

    @Autowired(required = false)
    private ISysDictDataService sysDictDataService;


    @GetMapping("/list")
    public AjaxResult list(@RequestParam("businessId") Long businessId) {
        //调用业务层查找商机跟进数据
        List<TbBusinessTrackRecord> list = iTbBusinessTrackRecordService.selectTbBusinessRecordList(businessId);
        //替换字典表中的数据
        for (TbBusinessTrackRecord businessTrackRecord : list) {
            String[] items = businessTrackRecord.getKeyItems().split(",");
            for (String item : items) {
                String dictLabel = sysDictDataService.selectDictLabel("communication_point", item);
                businessTrackRecord.getKeys().add(dictLabel);
            }
        }
        return AjaxResult.success(list);
    }



    @PostMapping
    public AjaxResult addBusinessRecord(@RequestBody BusinessTrackVO businessTrackVO){
        //创建一个商机跟进表的实体类对象，进行封装
        TbBusinessTrackRecord trackRecord = new TbBusinessTrackRecord();
        //将前端请求的数据赋值给商机跟进表
        BeanUtils.copyProperties(businessTrackVO,trackRecord);
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setCreateBy(SecurityUtils.getUsername());

        //创建一个商机表实体类对象，进行封装
        //如果商机跟进的过程中商机数据被修改了，那么线索实体类的数据也要跟着修改
        TbBusiness tbBusiness = new TbBusiness();
        //将前端请求的数据赋值给商机表
        BeanUtils.copyProperties(businessTrackVO,tbBusiness);
        tbBusiness.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());
        tbBusiness.setId(businessTrackVO.getBusinessId());

        return AjaxResult.success(iTbBusinessTrackRecordService.addTbBusinessRecord(tbBusiness,trackRecord));
    }
}
